package sphere;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.text.DecimalFormat;

public class SphereWindow extends JFrame implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField radiusIn, volumeOut, areaOut;
	private Sphere sph;
	private DecimalFormat f3 = new DecimalFormat("0.000");

	public SphereWindow() {
		super("Spheres: volume and surface area");
		sph = new Sphere(0, 0, 0);
		
		this.setSize(240, 160);
		JPanel view = new JPanel();
		view.setLayout(new GridLayout(0, 2, 10, 10));
		view.setBorder(new EmptyBorder(10, 10, 10, 10));
		view.add(new JLabel("Radius = ", SwingConstants.CENTER));
		radiusIn = new JTextField(8);
		radiusIn.setBackground(Color.yellow);
		radiusIn.addActionListener(this);
		view.add(radiusIn);
		
		view.add(new JLabel("Volume = ", SwingConstants.CENTER));
		volumeOut = new JTextField(8);
		volumeOut.setBackground(Color.white);
		volumeOut.addActionListener(this);
		view.add(volumeOut);
		
		view.add(new JLabel("Surface area = ", SwingConstants.CENTER));
		areaOut = new JTextField(8);
		areaOut.setBackground(Color.white);
		areaOut.addActionListener(this);
		view.add(areaOut);
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.setResizable(true);
		this.setContentPane(view);
		this.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JTextField t = (JTextField) e.getSource();
		double r = Double.parseDouble(t.getText());
		sph.setRadius(r);
		volumeOut.setText(String.valueOf(f3.format(sph.volume())));
		areaOut.setText(String.valueOf(f3.format(sph.surfaceArea())));
	}

}
